from flask import render_template
from flask import request
from frame.util.ResponseUtil import ResponseUtil
from .controller.Index import Index
from .controller.China import China
from frame.base import cache, limiter  # 从独立模块导入
from . import crawler_bp  # 导入当前包的蓝图


@crawler_bp.route('')
@crawler_bp.route('/')
@cache.cached(timeout=30)  # 缓存30秒
@limiter.limit("60 per minute")  # 一分钟内最多访问60次
def index():
    return render_template('index.html')  # 模板路径：apps/crawler/templates/index.html


@crawler_bp.route('/fetch/text')
@cache.cached(timeout=30, query_string=True)
@limiter.limit("60 per minute")
def scrape_handler():
    target_url = request.args.get('url')
    wait_time = request.args.get('time')
    html_content = Index().get_html_conntent(target_url, wait_time)
    return ResponseUtil.success({"content": html_content})


@crawler_bp.route('/stock/market')
@cache.cached(timeout=10, query_string=True)
@limiter.limit("10 per minute")
def stock_market_handler():
    target_url = request.args.get('url')
    # print(target_url)
    result = China().get_home_content(target_url)
    # print(result)
    return render_template('stock.html', **result)  # 模板路径：apps/crawler/templates/index.html
    # return ResponseUtil.success({"content": html_content})


@crawler_bp.route('/about')
def about():
    return "crawler About Page"
